package javaBasic;

public class Displacement_java {

    public static void main(String[] args) {
//        左移就是从一个数的二进制表示的数值从数值的右边起补充位移位数的0
//        右移与之相反
        int i = -1;
        System.out.println("Before << , i's value is " + i);
        System.out.println("i's binary string is " + Integer.toBinaryString(i));
        i <<= 10;
        System.out.println("After << , i's value is " + i);
        System.out.println("i's binary string is " + Integer.toBinaryString(i));
        System.out.println("========================================================");
//         对正数进行右移操作
        int i1 = 4992;
        System.out.println("Before >> , i1's value is " + i1);
        System.out.println("i1's binary string is " + Integer.toBinaryString(i1));
        i1 >>= 10;
        System.out.println("After >> , i1's value is " + i1);
        System.out.println("i1's binary string is " + Integer.toBinaryString(i1));
        // 对负数进行右移操作
        int i2 = -4992;
        System.out.println("Before >> , i2's value is " + i2);
        System.out.println("i2's binary string is " + Integer.toBinaryString(i2));
        i2 >>= 10;
        System.out.println("After >> , i2's value is " + i2);
        System.out.println("i2's binary string is " + Integer.toBinaryString(i2));


//原来，Java在处理byte、short、char的移位操作前，会先将其转型成int类型，然后在进行操作！
// 特别地，当对这三者使用<<=、>>=和>>>=时，其实是得到对移位后的int进行低位截断后的结果
        System.out.println("========================================================");

//        byte b = -1;
//        System.out.println("Before >> , b's value is " + b);
//        System.out.println("b's binary string is " + Integer.toBinaryString(b));
//        b >>>= 6;
//        System.out.println("After >> , b's value is " + b);
//        System.out.println("b's binary string is " + Integer.toBinaryString(b));


        byte b = -1;
        System.out.println("Before >> , b's value is " + b);
        System.out.println("b's binary string is " + Integer.toBinaryString(b));
        System.out.println("After >> , b's value is " + (b >>> 6));
        System.out.println("b's binary string is " + Integer.toBinaryString(b >>> 6));

//        无符号右移操作符 >>>
//        无符号右移操作符 >>>与>>类似，都是将数据转换为二进制数后右移若干位，
//        不同之处在于，不论负数与否，结果都是高位补零，低位丢弃。

//        如果移位的位数超过数值所占有的位数会怎样？
//      参考文章  https://zhuanlan.zhihu.com/p/98790031
//        对一个整型左移一位，相当于乘以2；右移一位，相当于除以2
    }

}
